package com.hyq.learn.api.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.net.SocketTimeoutException;

/**
 * @author heyuqin
 * @date 2025/11/17 17:11
 */
@Service
public class RetryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RetryService.class);


    public void selfRetry(String tag) {
        try {
            // retry重试方式是通过aop实现，这种调用自身的方式，无法代理自身，所以不会进行实际的重试
            testRetry(tag);
        } catch (SocketTimeoutException e) {
            // ignore
        }
    }


    @Retryable(retryFor = {SocketTimeoutException.class})
    public void testRetry(String tag) throws SocketTimeoutException {
        Thread thread = Thread.currentThread();
        LOGGER.info("thread Name:{}, method:{}", thread.getName(), thread.getStackTrace()[1].getMethodName());
        if ("retry".equals(tag)) {
            throw new SocketTimeoutException();
        }
    }
}
